﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DeepHash.CommandParser
{
    public class CommandOption
    {
        public enum CommandOptionType
        {
            ValueTypeString,
            ValueTypeInt,
            ValueTypeBoolean,
            ValueTypeStringSet,
            ValueTypeValidDirPath,
            ValueTypeValidFilePath,
        }

        public static CommandOption Opt(string name, CommandOptionType type, bool isOptional)
        {
            CommandOption opt = new CommandOption();
            opt.Name = name;
            opt.OptionType = type;
            opt.IsOptional = isOptional;
            return opt;
        }

        private string _name = string.Empty;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private CommandOptionType _optionType = CommandOptionType.ValueTypeString;
        public CommandOptionType OptionType
        {
            get { return _optionType; }
            set { _optionType = value; }
        }

        private bool _isOptional = false;
        public bool IsOptional
        {
            get { return _isOptional; }
            set { _isOptional = value; }
        }

        private string[] _stringSet = null;
        public string[] StringSet
        {
            get { return _stringSet; }
            set { _stringSet = value; }
        }

        internal int GetIntValue(string stringValue)
        {
            int ret = 0;
            if (int.TryParse(stringValue, out ret))
            {
                return ret;
            }
            return 0;
        }

        internal Path GetPathValue(string stringValue)
        {
            return null;
        }

        internal static bool ParseBoolean(string stringValue, out bool parsed)
        {
            if (bool.TryParse(stringValue, out parsed))
            {
                return true;
            }
            else
            {
                if (stringValue.Equals("y", StringComparison.InvariantCultureIgnoreCase))
                {
                    parsed = true;
                    return true;
                }
                if (stringValue.Equals("n", StringComparison.InvariantCultureIgnoreCase))
                {
                    parsed = false;
                    return true;
                }

                if (stringValue.Equals("yes", StringComparison.InvariantCultureIgnoreCase))
                {
                    parsed = true;
                    return true;
                }
                if (stringValue.Equals("no", StringComparison.InvariantCultureIgnoreCase))
                {
                    parsed = false;
                    return true;
                }

                if (stringValue.Equals("1"))
                {
                    parsed = true;
                    return true;
                }
                if (stringValue.Equals("0", StringComparison.InvariantCultureIgnoreCase))
                {
                    parsed = false;
                    return true;
                }

                return false;
            }

        }

        internal bool GetBoolValue (string stringValue)
        {
            bool ret = false;
            if (ParseBoolean(stringValue, out ret))
            {
                return ret;
            }
            return false;
        }

        public bool IsValidValue(string value)
        {
            if (this.OptionType == CommandOptionType.ValueTypeBoolean)
            {
                bool x = false;
                return ParseBoolean(value, out x);
            }
            if (this.OptionType == CommandOptionType.ValueTypeInt)
            {
                int x = 0;
                return int.TryParse(value, out x);
            }
            if (this.OptionType == CommandOptionType.ValueTypeString)
            {
                return !string.IsNullOrEmpty(value);
            }
            if (this.OptionType == CommandOptionType.ValueTypeStringSet)
            {
                foreach (string str in this.StringSet)
                {
                    if (string.Equals( str, value, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return true;
                    }
                }
                return false;
            }
            if (this.OptionType == CommandOptionType.ValueTypeValidDirPath)
            {
                Path path = new Path();
                path.FolderOrFile = Path.PathType.FolderOnly;
                path.PathString = value;
                path.IsRelativePathAccepted = true;
                if (path.IsValidPath())
                {
                    return true;
                }
                return false;
            }
            if (this.OptionType == CommandOptionType.ValueTypeValidFilePath)
            {
                Path path = new Path();
                path.FolderOrFile = Path.PathType.FileOnly;
                path.IsRelativePathAccepted = true;
                if (path.IsValidPath())
                {
                    return true;
                }
                return false;
            }
            return false;
        }

    }
}
